﻿using SnongPhoneShop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SnongPhoneShop.Controllers
{
    public class San_PhamController : Controller
    {
        //
        // GET: /San_Pham/

        StoredProcedure sp = new StoredProcedure();

        public ActionResult Dien_Thoai()
        {
            var ListDM = sp.GetListCatalogByTypeID(1);
            return View(ListDM);
        }

        public ActionResult Phu_Kien()
        {
            var ListDM = sp.GetListCatalogByTypeID(2);
            return View(ListDM);
        }

        public ActionResult CT_SanPham(string ID)
        {
            try
            {
                var ListCatalog = sp.GetListCatalogByTypeID(1);
                ViewBag.ListCatalog = new SelectList(ListCatalog, "MaDanhMuc", "TenDanhMuc");
                var SP = sp.GetProductByID(ID);
                HttpCookie newCookie;
                if (Request.Cookies["Recently"] == null) newCookie = new HttpCookie("Recently");
                else newCookie = Request.Cookies["Recently"];
                if (newCookie.Values.Count >= 10) newCookie.Values.Remove(newCookie.Values[0]);
                try
                {
                    newCookie.Values.Remove(ID);
                }
                catch { }
                newCookie.Values.Add(ID, ID);
                newCookie.Expires = DateTime.Now.AddDays(10);
                Response.Cookies.Add(newCookie);
                if (SP.DanhMucSanPham.MaLoaiDanhMuc == 1) return View("CT_DienThoai", SP);
                else return View("CT_PhuKien", SP);
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }
        }

        public ActionResult DM_SanPham(int ID)
        {
            var LDM = sp.GetListProductByCatalogID(ID);
            try
            {
                var DM = sp.GetCatalogByID(ID);
                ViewBag.Ten_DM = DM.TenDanhMuc;
                ViewBag.Loai_DM = DM.MaLoaiDanhMuc;
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }
            return View(LDM);
        }

        public ActionResult Dat_Hang(string ID)
        {
            if (ID == null)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                //if (Session["User"] != null)
                //{
                    if (sp.GetProductByID(ID) != null)
                    {
                        try
                        {
                            if (Session["Cart"] == null)
                            {
                                List<Cart> LC = new List<Cart>();
                                Session.Add("Cart", LC);
                            }
                            var LCart = (List<Cart>)Session["Cart"];
                            Cart SP = new Cart();
                            SP.MaSanPham = ID;
                            SP.SoLuong = 1;
                            var CurrSP = LCart.FirstOrDefault(m => m.MaSanPham.Equals(ID));
                            if (CurrSP == null)
                            {
                                LCart.Add(SP);
                            }
                            else
                            {
                                CurrSP.SoLuong++;
                            }
                            if (Session["User"] != null)
                            {
                                WriteCartToCookie(LCart);
                            }
                        }
                        catch { }
                        TempData["ToastEnable"] = true;
                        TempData["Message"] = "Thêm sản phẩm vào giỏ hàng thành công!<br>Click để xem giỏ hàng.";
                        TempData["LinkToast"] = Url.Action("Gio_Hang", "Tai_Khoan");
                    }
                    else return RedirectToAction("Index", "Home");
                //}
                //else
                //{
                //    TempData["ToastEnable"] = true;
                //    TempData["Message"] = "Bạn phải đăng nhập để thêm sản phẩm vào giỏ hàng.<br>Click để đăng nhập!";
                //    TempData["LinkToast"] = Url.Action("Dang_Nhap", "Tai_Khoan", new { UrlReferer = Request.UrlReferrer.OriginalString });
                //}
                return Redirect(Request.UrlReferrer.OriginalString);
            }
        }

        public ActionResult Bao_Gia_DT()
        {
            var ListDT = sp.GetListProductByTypeID(1).OrderBy(m => m.DanhMucSanPham.TenDanhMuc).ToList();
            return View(ListDT);
        }

        private void WriteCartToCookie(List<Cart> LCart)
        {
            //if (Request.Cookies["Login"] != null)
            //{
                HttpCookie NewCookie = new HttpCookie("Cart");
                foreach (var Item in LCart)
                {
                    NewCookie.Values.Add(Item.MaSanPham, Item.SoLuong.ToString());
                }
                NewCookie.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(NewCookie);
            //}
        }
    }
}
